<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>項目信息</title>
    <link th:replace="/comm/header :: header"/>
    <script type="text/javascript">
        $(function () {
            $("#dg").datagrid({
                onLoadSuccess: function (data) {
                    $('.res-easyui-linkbutton-binDing').linkbutton({text: '绑定车辆', plain: true, iconCls: 'fa fa-pencil'});
                    $('.role-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'fa fa-pencil'});
                    $('.role-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'fa fa-trash-o'});
                    $('.res-easyui-linkbutton-isStop').linkbutton({text: '停用', plain: true, iconCls: 'fa fa-pencil'});
                    $('.role-easyui-linkbutton-isCheck').linkbutton({text: '审核', plain: true, iconCls: 'fa fa-trash-o'});
                    $('.role-easyui-linkbutton-noCheck').linkbutton({text: '驳回', plain: true, iconCls: 'fa fa-trash-o'});
                    $('#dg').datagrid('fixRowHeight');
                }
            });
            $.extend($.fn.validatebox.defaults.rules, {
                telephone: {    //第三步,选中校验谁
                    validator: function (value) {    //第四步, 具体编写校验规则
                        var reg = /^1[3,5,7,8][0-9]{9}$/;
                        return reg.test(value);
                    },
                    message: '请输入正确的手机号!'   //第五步,如果输入内容不符合校验规则,出现的提示语.
                }
            });
        })

        function formatOp(val, row, index) {
            var str = '';
            if ($("#menuset").length > 0) {
                str += $.formatString('<a  href="javascript:void(0)" class="res-easyui-linkbutton-binDing success"  onclick="openBinDingDlg(\'{0}\');" >绑定车辆</a>', index);
            }
            if ($("#update").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-edit warning"  onclick="editFun(\'{0}\');" >编辑</a>', index);
            }
            if ($("#delete").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del error"   onclick="deleteFun(\'{0}\');" >删除</a>', row.id,row.sdate);
            }
            if ($("#isStop").length > 0 && row.isdelete) {
                str += $.formatString('<a  href="javascript:void(0)" class="res-easyui-linkbutton-isStop success"  onclick="isStopFun(\'{0}\');" >停用</a>', row.id);
            }
            if ($("#Check").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-isCheck warning"  onclick="isCheckFun(\'{0}\');" >审核</a>', row.id);
            }
            if ($("#noCheck").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-noCheck warning"  onclick="noCheckFun(\'{0}\');" >驳回</a>', row.id);
            }
            return str;
        }

        function rowStyler(index, row) {
            if (row.sex == 1) {
                return 'background-color:#6293BB;color:#fff;'; // return inline style
            } else {
                return 'color:green;';
            }
        }

        function formatter(value, row, index) {
            if (row.isstop) {
                return '已停用'
            } else {
                return '未停用'
            }
        }

        function ischeck(value, row, index) {
            if (row.ischeck == true) {
                return '已审核'
            } else {
                return '未审核'
            }
        }

        function status(value, row, index) {
            switch (row.cstatus) {
                case 13:
                    return '待审核';
                case 14:
                    return '已审核';
                case 15:
                    return '已绑定';
                case 16:
                    return '已停用';
                case 17:
                    return '已驳回';
            }
        }
        function isStopFun(id) {
            $.post("/pro/project/save", {id: id,isstop:true}, function (result) {
                if (result.flag) {
                    message("已停用！")
                    $("#dg").datagrid("reload");
                } else {
                    message(result.message)
                }
            })
        }

        function isCheckFun(id) {
            $.post("/pro/project/save", {id: id,ischeck:true}, function (result) {
                if (result.flag) {
                    message("已审核！")
                    $("#dg").datagrid("reload");
                } else {
                    message(result.message)
                }
            })
        }
        function noCheckFun(id) {
            $.post("/pro/project/save", {id: id,ischeck:false}, function (result) {
                if (result.flag) {
                    message("已驳回！")
                    $("#dg").datagrid("reload");
                } else {
                    message(result.message)
                }
            })
        }

        function menuSetFun(index) {
            $("#fm input").attr("disabled", true);
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid('getSelected');
            $("#dlg").dialog("open").dialog("setTitle", "详细信息");
            $("#fm").form("load", row);
        }

        function searchUser() {
            $("#dg").datagrid('load', {
                "cName": $("#s_userName").val()
            });
        }

        function closeUserDialog() {
            $("#dlg").dialog("close");
            $('#fm').form('clear');
        }
        function closeProDialog() {
            $("#dlg2").dialog("close");
            $('#fm2').form('clear');
        }
        function closeVehDialog() {
            $("#dlg3").dialog("close");
        }

        function saveUser() {
            $("#fm").form("submit", {
                url: "/pro/project/save",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.flag) {
                        message("保存成功");
                        $('#fm').form('clear');
                        $("#dg").datagrid("reload");
                        $("#dlg").dialog("close");

                    } else {
                        message(result.message)
                    }
                }
            });
        }
        function updatePro() {
            $("#fm2").form("submit", {
                url: "/pro/project/save",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.flag) {
                        message("保存成功");
                        $('#fm2').form('clear');
                        $("#dg").datagrid("reload");
                        $("#dlg2").dialog("close");
                    } else {
                        message(result.message)
                    }
                }
            });
        }

        function openBinDingDlg(index) {
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid('getSelected');
            $("#dlg3").dialog("open").dialog("setTitle", "绑定车辆");
            $("#dg3").datagrid({
                url: '/pro/project/getvehicle?proid='+row.id+'&eid='+row.eid,
                method: 'get',
                onLoadSuccess: function (data) {
                    //console.dir(data)
                    var allRows = $("#dg3").datagrid("getRows");
                    for (var i = 0; i < allRows.length; i++) {
                        var name = allRows[i].proid;
                        if (name!=null&&name!='') {
                            $("#dg3").datagrid("checkRow", i);
                        }
                    }
                }
            });
            $("#proId").val(row.id);
        }

        function binDing() {
            var proId= $("#proId").val();
            var vehIds = [];
            var selectedRows = $("#dg3").datagrid("getSelections");
            for (var i = 0; i < selectedRows.length; i++) {
                vehIds.push(selectedRows[i].id);
            }
            var vehIds1 = vehIds.join(",");
            $.post("/pro/project/binDingVeh", {proId: proId, vehIds:vehIds1}, function (result) {
                if (result.flag) {
                    message("绑定成功")
                } else {
                    message(result.message);
                }
            }, "json");
            $("#dlg3").dialog("close");
        }
        function openUserAddDialog() {
            $("#fm input").attr("disabled", false);
            $("#dlg").dialog("open").dialog("setTitle", "添加用户信息");

        }

        function compareDate(s1){
            return ((new Date(s1.replace(/-/g,"\/")))>(new Date()));
        }
        function editFun(index) {
            $("#fm input").attr("disabled", false);
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid('getSelected');
            if(!compareDate(row.sdate)){
                $("#dlg2").dialog("open").dialog("setTitle", "修改项目信息");
                $("#fm2").form("load", row);
            }else {
                $("#dlg").dialog("open").dialog("setTitle", "修改项目信息");
                $("#fm").form("load", row);
            }
        }

        function deleteFun(uid,sdate) {
            $.messager.confirm("系统提示", "您确定要删除这条数据吗?", function (r) {
                if (r) {
                    $.post("/pro/project/save", {id: uid, isdelete: true}, function (result) {
                        if (result.flag) {
                            message("删除成功！")
                            $("#dg").datagrid("reload");
                        } else {
                            message(result.message)
                        }
                    }, "json");
                }
            });
        }

        function saveRoleSet() {
            var userId = $("#userId").val();
            var selectedRows = $("#dg2").datagrid("getSelections");
            var strRolenames = [];
            var roleIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                strRolenames.push(selectedRows[i].cname);
                roleIds.push(selectedRows[i].id);
            }
            var rolenames = strRolenames.join(",");
            $.post("/sys/user/saveRoles", {
                cnames: rolenames,
                roleIds: roleIds.join(","),
                userId: userId
            }, function (result) {
                if (result.flag) {
                    closeRoleSetDialog();
                    $("#dg").datagrid("reload");
                    message("保存成功")
                } else {
                    message("提交失败，请联系管理员！");
                }
            }, "json");
        }

        function closeRoleSetDialog() {
            $("#dlg2").dialog("close");
        }
        function resetValue() {
            $("#dlg").form('clear');
        }
        function resetProValue() {
            $("#dlg").form('clear');
        }
    </script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="pro:info:vehicle">
    <input type="hidden" id="menuset"/>
    <!--    调度人，判断审核状态，绑定车辆，-->
</shiro:hasPermission>
<shiro:hasPermission name="pro:info:delete">
    <input type="hidden" id="delete"/>
    <!--    项目经理管理员，删除项目-->
</shiro:hasPermission>
<shiro:hasPermission name="pro:info:delete">
    <input type="hidden" id="isStop"/>
    <!--    项目经理，废弃项目-->
</shiro:hasPermission>
<shiro:hasPermission name="pro:info:edit">
    <input type="hidden" id="update"/>
    <!--    项目经理，修改项目-->
</shiro:hasPermission>
<shiro:hasPermission name="pro:info:ischeck">
    <input type="hidden" id="Check"/>
    <!--    超级管理员，判断审核状态，审核项目，-->
</shiro:hasPermission>
<shiro:hasPermission name="pro:info:ischeck">
    <input type="hidden" id="noCheck"/>
    <!--    超级管理员，判断审核状态，驳回审核，-->
</shiro:hasPermission>

<table id="dg" title="项目管理" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true" method="get"
       url="/pro/project/list" fit="true" toolbar="#tb">
    <thead>
    <th field="id" width="20" align="center" hidden="true">ID</th>
    <th field="ccode" width="50" align="center" hidden>项目编号</th>
    <th field="cname" width="50" align="center">项目名称</th>
    <th field="saddress" width="50" align="center">起始地</th>
    <th field="eaddress" width="50" align="center">目的地</th>
    <th field="cdesc" width="80" align="center" hidden>项目描述</th>
    <th field="price" width="80" align="center">单趟运输价格</th>
    <th field="sdate" width="80" align="center">项目开始时间</th>
    <th field="edate" width="80" align="center" hidden>项目结束时间</th>
    <th field="stotals" width="80" align="center">预设趟数</th>
    <th field="etotals" width="80" align="center">完成趟数标记</th>
    <th field="eid" width="80" align="center" hidden>企业ID</th>
    <th field="isstop" width="50" align="center" formatter="formatter" styler="rowStyler">是否停用</th>
    <th field="ischeck" width="50" align="center" formatter="ischeck" styler="rowStyler">审核状态</th>
    <th field="cstatus" width="80" align="center" formatter="status" styler="rowStyler">项目状态</th>
    <th field="createtime" width="80" align="center" hidden>创建时间</th>
    <th field="lasttime" width="80" align="center" hidden>修改时间</th>
    <th field="aa" width="200" align="center" formatter="formatOp">操作</th>
    </thead>
</table>

<div id="tb">
    <shiro:hasPermission name="pro:info:add">
        <div style="display: inline-block;">
            <a href="javascript:openUserAddDialog()" class="easyui-linkbutton primary" iconCls="fa fa-plus"
               plain="true">添加</a>
        </div>
    </shiro:hasPermission>
    <shiro:hasPermission name="pro:info:search">
        <div style="display: inline-block;float: right;font-size: 16px;text-align: center">
            &nbsp;项目名：&nbsp;<input type="text" id="s_userName" size="20" class="easyui-validatebox easyui-textbox"
                                   data-options="validType:'length[1,10]',iconAlign:'left', iconCls:'fa fa-user'"
                                   onkeydown="if(event.keyCode==13) searchUser()"/>
            <a href="javascript:searchUser()" class="easyui-linkbutton primary" iconCls="fa fa-search"
               plain="true">搜索</a>
        </div>
    </shiro:hasPermission>
</div>

<div id="dlg" class="easyui-dialog" style="width: 450px;height: 550px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <input type="hidden" name="id"/>
        <table cellspacing="8px">
            <tr>
                <td>项目名称：</td>
                <td>
                    <input type="text" id="cname" name="cname" class="easyui-validatebox easyui-textbox"
                           style="height: 35px;width: 200px" required="true"
                    />
                </td>
            </tr>
            <tr>
                <td>起始地：</td>
                <td>
                    <input type="text" id="saddress" name="saddress" class="easyui-validatebox easyui-textbox"
                           style="height: 35px;width: 200px" required="true"
                    />
                </td>
            </tr>
            <tr>
                <td>目的地：</td>
                <td>
                    <input type="text" id="eaddress" name="eaddress" class="easyui-textbox"
                           style="height: 35px;width: 200px" required="true"/>
                </td>
            </tr>
            <tr>
                <td>开始时间：</td>
                <td>
                    <input id="dd" name="sdate" type="text" class="easyui-datebox" required="required">
                </td>
            </tr>
            <tr>
                <td>单趟运输价格：</td>
                <td>
                    <input type="text" id="price" name="price" class="easyui-validatebox easyui-textbox"
                           style="height: 35px;width: 200px" required="true"/>
                </td>
            </tr>
            <tr>
                <td valign="top">预设趟数：</td>
                <td>
                    <input type="text" id="stotals" name="stotals" class="easyui-validatebox easyui-textbox"
                           style="height: 35px;width: 200px" required="true"/>
                </td>
            </tr>
            <tr>
                <td>项目描述：</td>
                <td>
                    <textarea rows="10" cols="50" id="cdesc" prompt="企业描述信息" name="cdesc" class="easyui-textarea"
                              style="width:330px"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg2" class="easyui-dialog" style="width: 450px;height: 550px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons2" data-options="onClose:function(){resetProValue()}">
    <form id="fm2" method="post">
        <input type="hidden" name="id"/>
        <table cellspacing="8px">
            <tr>
                <td>项目名称：</td>
                <td>
                    <input type="text"  name="cname" class="easyui-validatebox easyui-textbox"
                           style="height: 35px;width: 200px" required="true"
                    />
                </td>
            </tr>
            <tr>
                <td>项目描述：</td>
                <td>
                    <textarea rows="10" cols="50"  prompt="企业描述信息" name="cdesc" class="easyui-textarea"
                              style="width:330px"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg3" class="easyui-dialog" style="width: 500px;height: 350px;"
     closed="true" buttons="#dlg-buttons3">
    <input  id="proId" name="proId" hidden/>
    <table id="dg3" class="easyui-datagrid" fitColumns="true" method="post" fit="true">
        <thead>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="20" align="left" >车辆id</th>
        <th field="idcard" width="20" align="left" >车牌号</th>
        <th field="length" width="100" align="left">长度</th>
        <th field="width" width="200" align="left">车宽</th>
        <th field="tonnage" width="20" align="left" >吨位</th>
        <th field="length" width="100" align="left">长度</th>
        <th field="phone" width="200" align="left">队长电话</th>
        </thead>
    </table>
</div>
<div id="dlg-buttons">
    <a href="javascript:saveUser()" class="easyui-linkbutton" iconCls="fa fa-check">保存</a>
    <a href="javascript:closeUserDialog()" class="easyui-linkbutton" iconCls="fa fa-times">关闭</a>
</div>

<div id="dlg-buttons2">
    <a href="javascript:updatePro()" class="easyui-linkbutton" iconCls="fa fa-check">保存</a>
    <a href="javascript:closeProDialog()" class="easyui-linkbutton" iconCls="fa fa-times">关闭</a>
</div>

<div id="dlg-buttons3">
    <a href="javascript:binDing()" class="easyui-linkbutton" iconCls="fa fa-check">保存</a>
    <a href="javascript:closeVehDialog()" class="easyui-linkbutton" iconCls="fa fa-times">关闭</a>
</div>

</body>
</html>